<?php

namespace app\http\middleware;

use app\hander\HelpHander;

class Check
{
	public function handle($request, \Closure $next)
	{
		$header = $request->header();
		$token  = ( isset($header['token']) && !empty($header['token']) ) ? $header['token'] : input('token');
		$osType = ( isset($header['ostype']) && !empty($header['ostype']) ) ? $header['ostype'] : input('ostype', 'web');
		//判断设备码和操作系统
		if (!$token) {
			HelpHander::Response('not found token or osType', 401);
		}
		//操作系统只能为 ios、android、web
		if (!in_array($osType, [
			'web',
			'android',
			'ios'
		])) {
			HelpHander::Response('error osType', 401);
		}
		//根据设备码生成唯一token
		$token                = create_token($token);
		$request->token       = $token;
		$request->ostype      = $osType;
		$request->tokenPrefix = 'token:' . $osType . ':' . $token;
		return $next($request);
	}
}
